﻿using KinonekoSoftware.Studio.Enums;

namespace KinonekoSoftware.Studio.Pages.Notions
{
    public sealed class StoryPointEditorViewModel : TabViewModelCore
    {
        private readonly StoryPointEditorContext _context;

        private InspirationOrientedEditorViewModel _inspirationOriented;
        private StoryPointOrientedEditorViewModel  _storyPointOriented;
        private StoryPointSeparateEditorViewModel  _separateOriented;
        private StoryPointEditorSub                _subView;

        private readonly EnumMaster<StoryPointEditorMode>       _mode;
        private readonly EnumSingleSelect<StoryPointEditorMode> _isSeparateMode;
        private readonly EnumSingleSelect<StoryPointEditorMode> _isStoryPointOrientedMode;
        private readonly EnumSingleSelect<StoryPointEditorMode> _isInspirationOrientedMode;

        public StoryPointEditorViewModel()
        {
            _context                   = new StoryPointEditorContext();
            _mode                      = new EnumMaster<StoryPointEditorMode>();
            _isSeparateMode            = _mode.SingleSelect(nameof(IsSeparateMode), StoryPointEditorMode.Separate, RaiseUpdated, OnModeChanged);
            _isStoryPointOrientedMode  = _mode.SingleSelect(nameof(IsStoryMode), StoryPointEditorMode.Story, RaiseUpdated, OnModeChanged);
            _isInspirationOrientedMode = _mode.SingleSelect(nameof(IsInspirationMode), StoryPointEditorMode.Inspiration, RaiseUpdated, OnModeChanged);
        }

        private void OnModeChanged(StoryPointEditorMode mode)
        {
            SubView = GetDifferenceView();
        }

        private StoryPointEditorSub GetDifferenceView()
        {
            switch (_mode.Value)
            {
                case StoryPointEditorMode.Separate:
                    _separateOriented ??= new StoryPointSeparateEditorViewModel(_context);
                    return _separateOriented;
                case StoryPointEditorMode.Inspiration:
                    _inspirationOriented ??= new InspirationOrientedEditorViewModel(_context);
                    return _inspirationOriented;
                default:
                    _storyPointOriented ??= new StoryPointOrientedEditorViewModel(_context);
                    return _storyPointOriented;
            }
        }


        public bool IsSeparateMode
        {
            get => _isSeparateMode.Value;
            set => _isSeparateMode.Value = value;
        }
        
        public bool IsStoryMode
        {
            get => _isStoryPointOrientedMode.Value;
            set => _isStoryPointOrientedMode.Value = value;
        }
        
        public bool IsInspirationMode
        {
            get => _isInspirationOrientedMode.Value;
            set => _isInspirationOrientedMode.Value = value;
        }


        public StoryPointEditorSub SubView
        {
            get => _subView;
            set => SetValue(ref _subView, value);
        }
    }
}